package org.jwcloud.project.module.zx.matClient.service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.matClient.service.repository.MatClientRepository;
import org.jwcloud.project.module.zx.matClient.service.repository.MatSystemRepository;
import org.jwcloud.project.module.zx.workTime.service.repository.MatWorkTimeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jw.mid.mm.comm.common.controller.BaseController;

@Service
@Transactional
public class MatClientServiceImpl extends BaseController implements MatClientService{
	
	@Autowired
    public RptUtService rptUtService;
	@Autowired
	public MatClientRepository matClientRepository;
	@Autowired
	public MatSystemRepository matSystemRepository;
	
	private static String frontCompWithZore(int sourceDate,int formatLength){
		String newString = String.format("%0"+formatLength+"d", sourceDate);
		return newString;
	}
	
	@Override
	public Resp<List<Map<String, String>>> queryWorkList(Map param) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Resp<List<Map<String, String>>> queryItemList_client(Map param) {
		// TODO Auto-generated method stub
		List<String> list;
		if(param.get("query_type") == null || ((List)param.get("query_type")).size() == 0) {
			if(param.get("_query_type") == null) {
				return Resp.error("参数不可为空");
			}
			list = Arrays.asList(((String)param.get("_query_type")).split(";"));
		}else {
			list = (List)param.get("query_type");
		}
		
		for(int i = 0; i < list.size(); i++) {
			list.set(i, "'" + list.get(i) + "'");
		}
		
		Map dao = new HashMap();
		dao.put("sql", StringUtils.join(list.toArray(), ","));
		return Resp.success(this.matClientRepository.queryItemList_client(dao));
	}
	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
	Random rm = new Random();
	@Override
	public Resp<Object> saveSb(Map param) {
		// TODO Auto-generated method stub
		
		String ywCode = "210106";
		ywCode = ywCode + formatter.format(new Date())
			+ frontCompWithZore((int)(rm.nextDouble() * 10000000), 8);
		
		param.put("yw_code", ywCode);
		
		String flag = this.matClientRepository.checkSaceSb(param);
		if("no".equals(flag)) {
			return Resp.error("该小区已申报");
		}
		
		try {
			this.matClientRepository.saveSb(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			//业务编码重复
			return Resp.error("系统繁忙，请稍后再试");
		}
		Map processParam = new HashMap();
		processParam.put("rel_base_id", param.get("new_id_"));
		this.matClientRepository.saveProcess(processParam);
		
		if(param.get("tswxxm") != null && ((List)param.get("tswxxm")).size() > 0) {
			Map subParam = new HashMap();
			subParam.put("rel_base_id", param.get("new_id_"));
			List tswxxm = (List)param.get("tswxxm");
			for(int i = 0; i < tswxxm.size(); i++) {
				subParam.put("tsws", tswxxm.get(i));
				this.matClientRepository.saveSb_tsws(subParam);
			}
		}
		
		Map tipMap = new HashMap();//监控表
		tipMap.put("id", param.get("new_id_"));
		tipMap.put("next_state", "10");
		this.matSystemRepository.go_state_insertProcessTip(tipMap);
		
		return Resp.success(ywCode);
	}

}
